package handler

import (
	"github.com/gin-gonic/gin"
	"user_admin/global"
	"user_admin/internal/request"
	"user_admin/models"
)

func UseRegister(c *gin.Context) {
	var register request.Register

	if err := c.ShouldBind(&register); err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "获取失败",
			"data": nil,
		})
		return
	}

	user := &models.User{
		Mobile:   register.Mobile,
		Password: register.Password,
		NickName: register.NickName,
	}
	err := global.DB.Debug().Create(&user).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "注册失败",
			"data": nil,
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "注册成功",
		"data": user.ID,
	})

}
func UserLists(c *gin.Context) {
	var list request.UserList
	if err := c.ShouldBind(&list); err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "获取失败",
			"data": nil,
		})
		return
	}

	var user []*models.User
	err := global.DB.Debug().Find(&user).Error
	global.RDB.HSet(global.RDB.Context(), "users_list", user)

	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "查询失败",
			"data": nil,
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "查询成功",
		"data": user,
	})

}
